<?php

namespace App\Admin\Controllers;

use App\Models\Brand;
use App\Models\Type;
use App\Models\Standard;
use App\Models\Vendor;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Models\Channel;
use App\Models\Product;
use App\Models\Category;
use App\Models\AttributeGroup;
use Encore\Admin\Layout\Content;
use App\Admin\Actions\ProductAction\Skus;
use App\Admin\Actions\ProductAction\Slides;
use App\Admin\Actions\ProductAction\Images;
use App\Admin\Actions\ProductAction\Restore;
use Illuminate\Support\Facades\Storage;
use Encore\Admin\Controllers\AdminController;
use App\Services\MakeProductThumbnail\MakeProductThumbnail;

class ProductsController extends AdminController
{
    protected $title = '商品列表';

	protected function product($id = null)
	{
		if ($id and ($product = Product::find($id))) {
			return $product;
		}
		return null;
	}

    protected function grid()
    {
        $grid = new Grid(new Product());
        $grid->model()->orderByDesc('id');

        $grid->column('id', 'ID');
        $grid->column('image', '封面')->display(function ($value) {
	        if (! Storage::exists($value)) {
		       // $this->update(['cover' => null]);
		        return '';
	        } else {
		        return _grid_image($value);
	        }
        });

        $grid->column('title', '名称')->limit(50);
        $grid->column('price', '价格');
        $grid->column('age', '车龄');
        $grid->column('mileage', '里程');
        $grid->column('times', '过户次数');
       	$grid->column('displacement', '排量');

      
        $grid->column('is_open', '上架')->bool();
        $grid->column('is_sale', '已售')->bool();
        $grid->column('is_hidden', '隐藏')->bool();

        $grid->disableRowSelector();
        $grid->disableExport();
        $this->gridFilter($grid);
        $this->gridActions($grid);

        return $grid;
    }

    protected function gridActions(Grid $grid)
    {
	    $grid->actions(function (Grid\Displayers\Actions $actions) {
	    	$actions->disableView();
	    	$actions->disableDelete();
		    if (request('_scope_') == 'trashed') {
			    $actions->add(new Restore());
		    }
		    /*$actions->add(new Skus());
		    $actions->add(new Slides());
		    $actions->add(new Images());*/
	    });
    }

    protected function gridFilter(Grid $grid)
    {
	    $grid->filter(function (Grid\Filter $filter) {
		    $filter->expand();

		    $filter->column(1/2, function (Grid\Filter $filter) {
		    	$filter->disableIdFilter();
			    $filter->equal('id', '商品ID');
			    $filter->like('title', '标题查询');

		    });

		    $filter->column(1/2, function (Grid\Filter $filter) {
			    $filter->like('brand_id', '品牌查询')->select(Brand::all()->pluck('name', 'id'));
		    });

	    	$filter->scope('sale_on', '上架商品')->where('is_open', true);
	    });
    }

	public function edit($id, Content $content)
	{
		return $content
			->title($this->title())
			->description($this->description['edit'] ?? trans('admin.edit'))
			->body($this->form($id)->edit($id));
	}

	protected function form($id = null)
    {
        $form = new Form(new Product());

        $product = $this->product($id);
       // $this->formFilesCheck($id);

        $form->tab('基础信息', function ($form) {
	        $this->formBase($form);
        });

	    $this->formTools($form);
        $this->formFooter($form);

        //$this->formSaved($form, $id);
        return $form;
    }

    protected function formFilesCheck($id = null)
    {
	    if (!$product = $this->product($id)) {return;}

	    if (! Storage::exists($product->cover)) {
		    $product->cover = null;
		    $product->save();
	    }

	    foreach ($product->skus as $sku) {
		    if (! Storage::exists($sku->pic)) {
			    $sku->pic = null;
			    $sku->save();
		    }
	    }

	    foreach ($product->slides as $slide) {
		    if (! Storage::exists($slide->slide)) {
			    $slide->delete();
		    }
	    }

	    foreach ($product->images as $image) {
		    if (! Storage::exists($image->image)) {
			    $image->delete();
		    }
	    }
    }

    protected function formCategory(Form $form)
    {
	    $form->select('category_id', '所属分类')
		    ->options(Category::selectOptions())
		    ->rules(['nullable', function ($attribute, $value, $fail) {
			    if ($value and Category::isParent($value)) {
				    return $fail('请选择末级分类');
			    }
			    return true;
		    }]);
    }

    protected function formBase(Form $form)
    {
	    

	    $form->text('title', '商品标题')->rules('required');

	    $form->image('image', '封面图片')
		    ->uniqueName()
		    ->creationRules('required');

	    $form->textarea('description', '商品说明')
		    ->help('非必填项');

	    $form->select('brand_id','品牌')->options(Brand::all()->pluck('name', 'id'));
	    $form->select('type_id','车辆类型')->options(Type::all()->pluck('name', 'id'));
	    $form->select('standard_id','环保标准')->options(Standard::all()->pluck('name', 'id'));

	    $form->number('price', '价格')
		    ->min(1)->default(1);
		$form->number('age', '车龄')->default(1);
		$form->number('mileage', '里程')->default(1);
		$form->number('times', '过户次数')->min(0)->default(1);
		$form->currency('displacement', '排量')->default(1);
		    
		
	    $form->radio('is_open', '上架')->options(config('site.bool'))->default(false);
	    $form->radio('is_sale', '已售')->options(config('site.bool'))->default(false);
	    $form->radio('is_hidden', '隐藏')->options(config('site.bool'))->default(false);

	    $form->number('order', '排序')
		    ->min(1)->default(1)
		    ->help('NOTE: 仅做相对排序')
		    ->rules('required|regex:/[1-9]+/', [
			    'required' => '显示顺序为必填项',
			    'regex' => '显示顺序只能为正整数',
		    ]);
		
    }

   
	protected function formTools(Form $form)
	{
		$form->tools(function (Form\Tools $tools) use ($form) {
			$tools->disableView();
			$tools->disableDelete();
		});
	}

	protected function formFooter(Form $form)
	{
		$form->footer(function (Form\Footer $footer) {
			$footer->disableViewCheck();
			$footer->disableCreatingCheck();
			$footer->disableEditingCheck();
		});
	}

	
}
